package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.dto.HisCaseDto;
import com.woniu.dto.PageDTO;
import com.woniu.moudle.HisCase;
import com.woniu.mapper.HisCaseMapper;
import com.woniu.param.AddHisCaseParam;
import com.woniu.param.ListHisCaseParam;
import com.woniu.param.ModifyHisCaseParam;
import com.woniu.param.ModifyStatusHisCaseParam;
import com.woniu.service.HisCaseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.util.CopyBeanUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author w
 * @since 2021-11-11
 */
@Service
public class HisCaseServiceImpl extends ServiceImpl<HisCaseMapper, HisCase> implements HisCaseService {
    @Resource
    private  HisCaseMapper hisCaseMapper;
    //添加病历
    @Override
    public HisCaseDto generateMedicalRcords(AddHisCaseParam addHisCaseParam) {
        HisCase hisCase = new HisCase();
        BeanUtils.copyProperties(addHisCaseParam,hisCase);
        hisCaseMapper.insert(hisCase);
        HisCaseDto hisCaseDto = new HisCaseDto();
        BeanUtils.copyProperties(hisCase,hisCaseDto);
        return hisCaseDto;
    }
    //删除病历
    @Override
    public Boolean deleteMedicalRecord(ArrayList<Integer> ids) {
        int i = hisCaseMapper.deleteBatchIds(ids);
        return i>0?true:false;
    }
    //查询病历
    @Override
    public PageDTO<HisCaseDto> queryMedicalRecords(ListHisCaseParam listHisCaseParam) {
        QueryWrapper<HisCase> hisCaseQueryWrapper = new QueryWrapper<>();

        if(listHisCaseParam!=null){//请求类不为空
            if(listHisCaseParam.getDiseaseName()!=null){
                hisCaseQueryWrapper.eq("disease_name",listHisCaseParam.getDiseaseName());
            }
            if(listHisCaseParam.getCaseId()!=null){
                hisCaseQueryWrapper.eq("case_id",listHisCaseParam.getCaseId());
            }
            if(listHisCaseParam.getCaseRemarks()!=null){
                hisCaseQueryWrapper.eq("case_remarks",listHisCaseParam.getCaseRemarks());
            }
            if(listHisCaseParam.getCaseState()!=null){
                hisCaseQueryWrapper.eq("case_state",listHisCaseParam.getCaseState());
            }
            if(listHisCaseParam.getCaseSymptoms()!=null){
                hisCaseQueryWrapper.eq("case_symptoms",listHisCaseParam.getCaseSymptoms());
            }
            if(listHisCaseParam.getCaseTime()!=null){
                hisCaseQueryWrapper.eq("case_time",listHisCaseParam.getCaseTime());
            }
            if(listHisCaseParam.getCaseTreatment()!=null){
                hisCaseQueryWrapper.eq("case_treatment",listHisCaseParam.getCaseTreatment());
            }
            if(listHisCaseParam.getDiseaseId()!=null){
                hisCaseQueryWrapper.eq("disease_id",listHisCaseParam.getDiseaseId());
            }
            if(listHisCaseParam.getCaseDiseaseName()!=null){
                hisCaseQueryWrapper.eq("case_disease_name",listHisCaseParam.getCaseDiseaseName());
            }
            if(listHisCaseParam.getDoctorId()!=null){
                hisCaseQueryWrapper.eq("doctor_id",listHisCaseParam.getDoctorId());
            }
            if(listHisCaseParam.getPatientId()!=null){
                hisCaseQueryWrapper.eq("patient_id",listHisCaseParam.getPatientId());

            }


        }

        Page<HisCase> page=page(new Page<HisCase>(listHisCaseParam.getPageParam().getPage(),listHisCaseParam.getPageParam().getPageSize()),hisCaseQueryWrapper);



        return PageDTO.bulid(page,HisCaseDto.class);
    }
    //修改病历状态
    @Override
    public HisCaseDto modifyMedicalRecordStatus(ModifyStatusHisCaseParam modifyStatusHisCaseParam) {
        HisCase hisCase = new HisCase();
        hisCase.setCaseId(modifyStatusHisCaseParam.getCaseId());
        hisCase.setCaseState(1);
        hisCase.setCaseTime(new Date());
        hisCaseMapper.updateById(hisCase);
        HisCaseDto hisCaseDto = new HisCaseDto();
        BeanUtils.copyProperties(hisCase,hisCaseDto);
        return hisCaseDto;
    }

    @Override
    public HisCaseDto modifyMedicalRecord(ModifyHisCaseParam modifyHisCaseParam) {
        UpdateWrapper<HisCase> hisCaseUpdateWrapper = new UpdateWrapper<>();
//        if(modifyHisCaseParam.getCaseDiseaseName()!=null){
//            hisCaseUpdateWrapper.set(true,"case_disease_name",modifyHisCaseParam.getCaseDiseaseName());
//        }
//        if(modifyHisCaseParam.getCaseRemarks()!=null){
//            hisCaseUpdateWrapper.set(true,"case_remarks",modifyHisCaseParam.getCaseRemarks());
//        }
//        if(modifyHisCaseParam.getCaseSymptoms()!=null){
//            hisCaseUpdateWrapper.set(true,"case_symptoms",modifyHisCaseParam.getCaseSymptoms());
//        }
//        if(modifyHisCaseParam.getCaseTreatment()!=null){
//            hisCaseUpdateWrapper.set(true,"case_treatment",modifyHisCaseParam.getCaseTreatment());
//        }
//        if(modifyHisCaseParam.getDiseaseId()!=null){
//            hisCaseUpdateWrapper.set(true,"disease_id",modifyHisCaseParam.getDiseaseId());
//        }
//        if(modifyHisCaseParam.getDiseaseName()!=null){
//            hisCaseUpdateWrapper.set(true,"disease_name",modifyHisCaseParam.getDiseaseName());
//        }
//        if(modifyHisCaseParam.getDoctorId()!=null){
//            hisCaseUpdateWrapper.set(true,"doctor_id",modifyHisCaseParam.getDoctorId());
//        }
//        if(modifyHisCaseParam.getDoctorName()!=null){
//            hisCaseUpdateWrapper.set(true,"doctor_name",modifyHisCaseParam.getDoctorName());
//        }
        HisCase hisCase = new HisCase();
        BeanUtils.copyProperties(modifyHisCaseParam,hisCase);

        hisCaseMapper.updateById(hisCase);
        HisCaseDto hisCaseDto = new HisCaseDto();

        BeanUtils.copyProperties(hisCase,hisCaseDto);
        return hisCaseDto;
    }
}
